package com.copm.demonstrationsystem.controller.introductionfeatures;

import com.copm.demonstrationsystem.commons.response.ResponseStatus;
import com.copm.demonstrationsystem.commons.util.ControllerUtils;
import com.copm.demonstrationsystem.commons.util.ImageUtils;
import com.copm.demonstrationsystem.config.WebConfig;
import com.copm.demonstrationsystem.dto.introductionfeatures.landscapeAndFacility.LandscapeTypeImageReceiveDto;
import com.copm.demonstrationsystem.service.introductionfeatures.LandscapeService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @Author: Yuan Baiyu
 * @Date: Created in 15:26 2018/4/23
 */

@RestController
@RequestMapping("/landscape")
@CrossOrigin
public class LandscapeController{

    @Autowired
    LandscapeService landscapeService;

    //1.0、获取园林景观接口，包括里面的所有的图片
    @ApiOperation("获取所有的图片")
    @GetMapping("/images")
    ResponseStatus getLandscapeTypes() {

        return ControllerUtils.fillResponse(new ResponseStatus(), landscapeService.getLandscapeTypes());
    }

    //1.1、位置类型添加接口
    @ApiOperation("位置类型添加接口")
    //请求参数
    @ApiImplicitParam(name = "name", value = "新的位置类型的名称"
            , dataType = "String", paramType = "query", required = true)
    @PostMapping("/placetype")
    ResponseStatus addPlaceType(@RequestParam("name") String name) {
        return ControllerUtils.fillResponse(new ResponseStatus(), landscapeService.addPlaceType(name));
    }

    //1.2、位置类型删除接口
    @ApiOperation("位置类型删除接口")
    //请求参数
    @ApiImplicitParam(name = "id", value = "位置类型的ID"
            , dataType = "Long", paramType = "path", required = true)
    @DeleteMapping("/placetype/{id}")
    ResponseStatus deletePlaceType(@PathVariable("id") Long id) {
        return ControllerUtils.fillResponse(new ResponseStatus(), landscapeService.deletePlaceType(id));
    }

    //1.3、获取全部位置类型接口
    @ApiOperation("获取全部位置类型接口")
    //请求参数
    @GetMapping("/placetype")
    ResponseStatus getPlaceTypes() {
        return ControllerUtils.fillResponse(new ResponseStatus(), landscapeService.getPlaceTypes());
    }


    //3.1、图片添加接口
    @ApiOperation("图片添加接口")
    //请求参数
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "上传图片的form-data数据对象"
                    , dataType = "file", paramType = "form", required = true),
            @ApiImplicitParam(name = "placetypeId", value = "上传图片的位置类型ID"
                    , dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "kind", value = "上传图片的图片分类"
                    , dataType = "Long", paramType = "query", required = true)
    })
    @PostMapping("/image")
    ResponseStatus addLandscapeTypeImage(Long placetypeId, Long kind, MultipartFile file) {
        LandscapeTypeImageReceiveDto imageReceiveDto = new LandscapeTypeImageReceiveDto(placetypeId, kind);

        //图片格式校验
        Boolean suffixEnable = ImageUtils.checkSuffix(file.getOriginalFilename());
        if (!suffixEnable) {
            return new ResponseStatus(0, "图片格式不正确", null);
        }
        return ControllerUtils.fillResponse(new ResponseStatus(), landscapeService.addLandscapeTypeImage(imageReceiveDto, file));

    }

    //3.2、图片删除接口
    @ApiOperation("图片删除接口")
    //请求参数
    @ApiImplicitParam(name = "id", value = "图片的ID"
            , dataType = "Long", paramType = "path", required = true)
    @DeleteMapping("/image/{id}")
    ResponseStatus deleteLandscapeTypeImage(@PathVariable("id") Long id) {
        return ControllerUtils.fillResponse(new ResponseStatus(), landscapeService.deleteLandscapeTypeImage(id));
    }

    //修改“园林景观”页面toUrl信息
    @PostMapping("/tourl")
    @ApiOperation("修改toUrl")
    @ApiImplicitParam(name = "tourl", value = "新的toUrl"
            , dataType = "String", paramType = "query", required = true)
    public ResponseStatus modifyText(@RequestParam("tourl") String tourl) {
        return ControllerUtils.fillResponse(new ResponseStatus(),
                landscapeService.modifyLandscapeToUrl(tourl));
    }
}
